/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.hgugbk
 */
package almacen;


import resources.local_connection;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author pato
 */
public class Usuario extends javax.swing.JFrame {
    
    Connection con = null;
    PreparedStatement ps;
    local_connection conexion;
    ResultSet rs;
    static boolean otro = false;
    static Almacen pov;
   public Usuario() throws SQLException, ClassNotFoundException 
    {
        initComponents();
        setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.requestFocus();
        pov = new Almacen();
        getRootPane().setDefaultButton(jButton1);
        try {
                con = local_connection.getconnect();
            }
        catch (SQLException ex) 
            {
            Almacen.logger.log(Level.SEVERE, null, ex);}
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPasswordField1 = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Acceso a TeslaBEM Almacen (Beta)");
        setIconImage(new ImageIcon(getClass().getResource("/resources/logo_rayito.png")).getImage());

        jLabel1.setText("Codigo de Acceso:");

        jButton1.setText("Acceder");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Acceso:");

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/TeslaBEM_acceso.jpg"))); // NOI18N
        jLabel3.setMaximumSize(new java.awt.Dimension(200, 186));
        jLabel3.setMinimumSize(new java.awt.Dimension(200, 186));
        jLabel3.setPreferredSize(new java.awt.Dimension(2974, 928));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jButton1)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       try {
            ps = (PreparedStatement) con.prepareStatement("SELECT id_persona, nombre, categoria FROM personas WHERE pass = '" + String.valueOf(jPasswordField1.getPassword())+"' LIMIT 1");
            rs = ps.executeQuery();
            if (rs.next()) {
                int id;
                JOptionPane.showMessageDialog(this, "Bienvenido "+rs.getString(2));
                id = rs.getInt(1);
                pov.setUsuario(id);
                if(!otro){
                    pov.setLocationRelativeTo(null);
                    pov.setVisible(true);
                }
                else{
                    pov.dispose();      
                }
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(this, "Password incorrecto, intentelo de nuevo");
                jPasswordField1.setText("");
                jPasswordField1.requestFocus();               
            }
        } catch (SQLException ex) {
Almacen.logger.log(Level.SEVERE, null, ex);        }
    }//GEN-LAST:event_jButton1ActionPerformed
    
    public void setOtro(boolean n){
        otro = n;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
            * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
            */
            // WebLookAndFeel.install();
            UIManager.setLookAndFeel("de.centigrade.bizlaf.BizLookAndFeel");
            //</editor-fold>
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
Almacen.logger.log(Level.SEVERE, null, ex);        }

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {                
                    Usuario usuario= new Usuario();
                    usuario.setLocationRelativeTo(null);
                    usuario.setVisible(true);
                } catch (        SQLException | ClassNotFoundException ex) {
Almacen.logger.log(Level.SEVERE, null, ex);                }
               
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField jPasswordField1;
    // End of variables declaration//GEN-END:variables
    
    
    cambiarConexion miconexion;
}
